//============================================================================================
/**
 * @file	mb_obj.c
 * @brief	メールボックス画面 OBJ関連
 * @author	Hiroyuki Nakamura
 * @date	09.01.31
 */
//============================================================================================
#include "common.h"

#include "poketool/pokeicon.h"
#include "poketool/icongra/poke_icon.naix"

#include "mb_main.h"
#include "mb_obj.h"
#include "mailbox_gra.naix"


//============================================================================================
//	定数定義
//============================================================================================

// アクター表示フラグ
enum {
	ACT_OFF = 0,	// アクター非表示
	ACT_ON			// アクター表示
};

#define	CLACT_ID_COMMON		( 14320 )	// この画面で使用するセルアクターのID

// キャラリソースID
enum {
	CHR_ID_POKEICON1 = CLACT_ID_COMMON,	// ポケモンアイコン
	CHR_ID_POKEICON2,
	CHR_ID_POKEICON3,
	CHR_ID_MBOBJ,
	CHR_ID_MAX = CHR_ID_MBOBJ - CLACT_ID_COMMON + 1
};
// パレットリソースID
enum {
	PAL_ID_POKEICON = CLACT_ID_COMMON,	// ポケモンアイコン
	PAL_ID_MBOBJ,
	PAL_ID_MAX = PAL_ID_MBOBJ - CLACT_ID_COMMON + 1
};
// セルリソースID
enum {
	CEL_ID_POKEICON = CLACT_ID_COMMON,	// ポケモンアイコン
	CEL_ID_MBOBJ,
	CEL_ID_MAX = CEL_ID_MBOBJ - CLACT_ID_COMMON + 1
};
// セルアニメリソースID
enum {
	ANM_ID_POKEICON = CLACT_ID_COMMON,	// ポケモンアイコン
	ANM_ID_MBOBJ,
	ANM_ID_MAX = CEL_ID_MBOBJ - CLACT_ID_COMMON + 1
};

#define	CLA_H_NONE	( CLACT_U_HEADER_DATA_NONE )	// セルアクターヘッダーデータなし

#define	SUB_ACTOR_DISTANCE	( 512*FX32_ONE )		// サブ画面のOBJ表示Y座標オフセット

// パレット配置
#define	PALSIZ_POKEICON			( POKEICON_PAL_MAX )	// パレット数：ポケモンアイコン
#define	PALSIZ_MBOBJ			( 2 )					// パレット数：その他のＯＢＪ

#define	MAILREAD_CUR_PX		( 224 )		//「メールをよむ」のカーソルＸ座標
#define	MAILREAD_CUR_PY		( 176 )		//「メールをよむ」のカーソルＹ座標

#define POKEICON_PX		( 128 )		// ポケモンアイコン表示Ｘ座標
#define POKEICON_PY		( 160 )		// ポケモンアイコン表示Ｙ座標
#define POKEICON_SX		( 40 )		// ポケモンアイコン表示Ｘ間隔

#define	POKEICON_CGX_SIZE	( 0x20*4*4 )	// ポケモンアイコンのCGXサイズ


//============================================================================================
//	プロトタイプ宣言
//============================================================================================
static void ClactResManInit( MAILBOX_APP_WORK * appwk );
static void ClactResManExit( MAILBOX_APP_WORK * appwk );
static CATS_ACT_PTR ClactAdd( MAILBOX_APP_WORK * appwk, const TCATS_OBJECT_ADD_PARAM_S * prm );
static void ClactDel( MAILBOX_APP_WORK * appwk, u32 id );
static void ClactDelAll( MAILBOX_APP_WORK * appwk );

static void PokeIconArcHandleOpen( MAILBOX_APP_WORK * appwk );
static void PokeIconArcHandleClose( MAILBOX_APP_WORK * appwk );
static void PokeIconResLoad( MAILBOX_APP_WORK * appwk );
//static void PokeIconResFree( MAILBOX_APP_WORK * appwk );

static void MailBoxObjResLoad( MAILBOX_APP_WORK * appwk );


//============================================================================================
//	グローバル変数
//============================================================================================

// 検索ページのＯＢＪデータ
static const TCATS_OBJECT_ADD_PARAM_S ClaAddPrm[] =
{
	{	// 左矢印
		24, 176, 0,
		0, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MBOBJ, PAL_ID_MBOBJ, CEL_ID_MBOBJ, ANM_ID_MBOBJ, CLA_H_NONE, CLA_H_NONE },
		3, 0
	},
	{	// 右矢印
		56, 176, 0,
		1, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MBOBJ, PAL_ID_MBOBJ, CEL_ID_MBOBJ, ANM_ID_MBOBJ, CLA_H_NONE, CLA_H_NONE },
		3, 0
	},
	{	// カーソル
		64, 16, 0,
		5, 0, 1, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MBOBJ, PAL_ID_MBOBJ, CEL_ID_MBOBJ, ANM_ID_MBOBJ, CLA_H_NONE, CLA_H_NONE },
		1, 0
	},
	{	// 戻るボタン
		224, 176, 0,
		2, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MBOBJ, PAL_ID_MBOBJ, CEL_ID_MBOBJ, ANM_ID_MBOBJ, CLA_H_NONE, CLA_H_NONE },
		3, 0
	},

	{	// ポケモンアイコン１
		POKEICON_PX-POKEICON_SX*0, POKEICON_PY, 0,
		0, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_POKEICON1, PAL_ID_POKEICON, CEL_ID_POKEICON, ANM_ID_POKEICON, CLA_H_NONE, CLA_H_NONE },
		1, 0
	},
	{	// ポケモンアイコン２
		POKEICON_PX-POKEICON_SX*1, POKEICON_PY, 0,
		0, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_POKEICON2, PAL_ID_POKEICON, CEL_ID_POKEICON, ANM_ID_POKEICON, CLA_H_NONE, CLA_H_NONE },
		1, 0
	},
	{	// ポケモンアイコン３
		POKEICON_PX-POKEICON_SX*2, POKEICON_PY, 0,
		0, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_POKEICON3, PAL_ID_POKEICON, CEL_ID_POKEICON, ANM_ID_POKEICON, CLA_H_NONE, CLA_H_NONE },
		1, 0
	},
};




//--------------------------------------------------------------------------------------------
/**
 * セルアクター初期化
 *
 * @param	syswk		システムワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_Init( MAILBOX_SYS_WORK * syswk )
{
	u32	i;

	GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// MAIN DISP OBJ ON
	GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// SUB DISP OBJ ON

	PokeIconArcHandleOpen( syswk->app );
	ClactResManInit( syswk->app );

	MailBoxObjResLoad( syswk->app );
	PokeIconResLoad( syswk->app );

	for( i=0; i<MBMAIN_OBJ_MAX; i++ ){
		syswk->app->cap[i] = ClactAdd( syswk->app, &ClaAddPrm[i] );
	}

	if( syswk->app->mail_max <= MBMAIN_MAILLIST_MAX ){
		MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_ARROW_L, FALSE );
		MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_ARROW_R, FALSE );
	}else if( syswk->sel_page == 0 ){
		MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_ARROW_L, FALSE );
	}else{
		MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_ARROW_R, FALSE );
	}
	MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_POKEICON1, FALSE );
	MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_POKEICON2, FALSE );
	MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_POKEICON3, FALSE );
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクター解放
 *
 * @param	syswk		システムワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_Exit( MAILBOX_SYS_WORK * syswk )
{
//	PokeIconResFree( appwk );

	ClactDelAll( syswk->app );
	ClactResManExit( syswk->app );

	PokeIconArcHandleClose( syswk->app );
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクターアニメメイン
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_Main( MAILBOX_APP_WORK * appwk )
{
	u32	i;

	for( i=0; i<MBMAIN_OBJ_MAX; i++ ){
		if( appwk->cap[i] != NULL ){
			CATS_ObjectUpdateCap( appwk->cap[i] );
		}
	}
}


//--------------------------------------------------------------------------------------------
/**
 * リソースマネージャー初期化
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactResManInit( MAILBOX_APP_WORK * appwk )
{
	appwk->csp = CATS_AllocMemory( HEAPID_MAILBOX_APP );
	appwk->crp = CATS_ResourceCreate( appwk->csp );

	{
		TCATS_OAM_INIT	coi = {
			0, 128,
			0, 32,
			0, 128,
			0, 32,
		};
		TCATS_CHAR_MANAGER_MAKE ccmm = {
			1024,
			1024*64,
			1024*16,
			GX_OBJVRAMMODE_CHAR_1D_32K,
			GX_OBJVRAMMODE_CHAR_1D_32K
		};
		CATS_SystemInit( appwk->csp, &coi, &ccmm, 32 );
	}
	{
		TCATS_RESOURCE_NUM_LIST	crnl = {
			CHR_ID_MAX, PAL_ID_MAX, CEL_ID_MAX, ANM_ID_MAX, 0, 0
		};
		CATS_ClactSetInit( appwk->csp, appwk->crp, MBMAIN_OBJ_MAX );
		CATS_ResourceManagerInit( appwk->csp, appwk->crp, &crnl );
	}
	{
		CLACT_U_EASYRENDER_DATA * renddata = CATS_EasyRenderGet( appwk->csp );
		CLACT_U_SetSubSurfaceMatrix( renddata, 0, SUB_ACTOR_DISTANCE );
	}

}

//--------------------------------------------------------------------------------------------
/**
 * リソースマネージャー解放
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactResManExit( MAILBOX_APP_WORK * appwk )
{
	CATS_ResourceDestructor_S( appwk->csp, appwk->crp );
	CATS_FreeMemory( appwk->csp );
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクター追加
 *
 * @param	appwk	アプリワーク
 * @param	prm		追加データ
 *
 * @return	追加したセルアクターのポインタ
 */
//--------------------------------------------------------------------------------------------
static CATS_ACT_PTR ClactAdd( MAILBOX_APP_WORK * appwk, const TCATS_OBJECT_ADD_PARAM_S * prm )
{
	if( prm->d_area == NNS_G2D_VRAM_TYPE_2DMAIN ){
		return CATS_ObjectAdd_S( appwk->csp, appwk->crp, prm );
	}
	return CATS_ObjectAdd_S_SubDistance( appwk->csp, appwk->crp, prm, SUB_ACTOR_DISTANCE );
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクター削除
 *
 * @param	appwk	アプリワーク
 * @param	id		OBJ ID
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactDel( MAILBOX_APP_WORK * appwk, u32 id )
{
	if( appwk->cap[id] != NULL ){
		CATS_ActorPointerDelete_S( appwk->cap[id] );
		appwk->cap[id] = NULL;
	}
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクター全削除
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactDelAll( MAILBOX_APP_WORK * appwk )
{
	u32	i;

	for( i=0; i<MBMAIN_OBJ_MAX; i++ ){
		ClactDel( appwk, i );
	}
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクターアニメ変更
 *
 * @param	appwk	アプリワーク
 * @param	id		OBJ ID
 * @param	anm		アニメ番号
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_AnmSet( MAILBOX_APP_WORK * appwk, u32 id, u32 anm )
{
	CATS_ObjectAnimeFrameSetCap( appwk->cap[id], 0 );
	CATS_ObjectAnimeSeqSetCap( appwk->cap[id], anm );
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクターアニメ状態取得
 *
 * @param	appwk	アプリワーク
 *
 * @return	BOOL
 */
//--------------------------------------------------------------------------------------------
/*
BOOL ZKN2OBJ_AnmGet( ZKN2_WORK * wk, u32 id )
{
	return CATS_ObjectAnimeActiveCheckCap( wk->cap[id] );
}
*/

//--------------------------------------------------------------------------------------------
/**
 * セルアクター表示切り替え
 *
 * @param	appwk	アプリワーク
 * @param	id		OBJ ID
 * @param	flg		表示フラグ
 *
 * @return	none
 *
 * @li	flg = TRUE : 表示
 * @li	flg = FALSE : 非表示
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_Vanish( MAILBOX_APP_WORK * appwk, u32 id, BOOL flg )
{
	if( flg == TRUE ){
		CATS_ObjectEnableCap( appwk->cap[id], ACT_ON );
	}else{
		CATS_ObjectEnableCap( appwk->cap[id], ACT_OFF );
	}
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクター移動
 *
 * @param	appwk	アプリワーク
 * @param	id		OBJ ID
 * @param	px		Ｘ座標
 * @param	py		Ｙ座標
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_PosSet( MAILBOX_APP_WORK * appwk, u32 id, s16 px, s16 py )
{
	CATS_ObjectPosSetCap( appwk->cap[id], px, py );
}

//--------------------------------------------------------------------------------------------
/**
 * セルアクター半透明設定
 *
 * @param	appwk	アプリワーク
 * @param	id		OBJ ID
 * @param	flg		ON/OFFフラグ
 *
 * @return	none
 *
 * @li	flg : TRUE = ON, FALSE = OFF
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_BlendModeSet( MAILBOX_APP_WORK * appwk, u32 id, BOOL flg )
{
	if( flg == TRUE ){
		CATS_ObjectObjModeSetCap( appwk->cap[id], GX_OAM_MODE_XLU );
	}else{
		CATS_ObjectObjModeSetCap( appwk->cap[id], GX_OAM_MODE_NORMAL );
	}
}


//============================================================================================
//	ポケモンアイコン
//============================================================================================

//--------------------------------------------------------------------------------------------
/**
 * ポケモンアイコンのアーカイブハンドル取得
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void PokeIconArcHandleOpen( MAILBOX_APP_WORK * appwk )
{
	appwk->pokeicon_ah = ArchiveDataHandleOpen( ARC_POKEICON, HEAPID_MAILBOX_APP );
}

//--------------------------------------------------------------------------------------------
/**
 * ポケモンアイコンのアーカイブハンドル破棄
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void PokeIconArcHandleClose( MAILBOX_APP_WORK * appwk )
{
	ArchiveDataHandleClose( appwk->pokeicon_ah );
}

//--------------------------------------------------------------------------------------------
/**
 * ポケモンアイコンリソース読み込み
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void PokeIconResLoad( MAILBOX_APP_WORK * appwk )
{
	u32	i;

	for( i=CHR_ID_POKEICON1; i<=CHR_ID_POKEICON3; i++ ){
		CATS_LoadResourceCharArcH(
			appwk->csp, appwk->crp, appwk->pokeicon_ah,
			PokeIconCgxArcIndexGetByMonsNumber(0,0,0),
			0, NNS_G2D_VRAM_TYPE_2DSUB, i );
	}
	CATS_LoadResourcePlttArcH(
		appwk->csp, appwk->crp,
		appwk->pokeicon_ah, PokeIconPalArcIndexGet(), 0,
		PALSIZ_POKEICON, NNS_G2D_VRAM_TYPE_2DSUB, PAL_ID_POKEICON );
	CATS_LoadResourceCellArcH(
		appwk->csp, appwk->crp,
		appwk->pokeicon_ah, PokeIconCellArcIndexGet(), 0, CEL_ID_POKEICON );
	CATS_LoadResourceCellAnmArcH(
		appwk->csp, appwk->crp,
		appwk->pokeicon_ah, PokeIconAnmCellArcIndexGet(), 0, ANM_ID_POKEICON );
}

/*
static void PokeIconResFree( MAILBOX_APP_WORK * appwk )
{
	u32	i;

	for( i=CHR_ID_POKEICON1; i<=CHR_ID_POKEICON3; i++ ){
		CATS_FreeResourceChar( appwk->crp, i );
	}
	CATS_FreeResourcePltt( appwk->crp, PAL_ID_POKEICON );
	CATS_FreeResourceCell( appwk->crp, CEL_ID_POKEICON );
	CATS_FreeResourceCellAnm( appwk->crp, ANM_ID_POKEICON );
}
*/

//--------------------------------------------------------------------------------------------
/**
 * ポケモンアイコン表示
 *
 * @param	syswk	システムワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_PokeIconPut( MAILBOX_SYS_WORK * syswk )
{
	MAIL_DATA * md;
	NNSG2dCharacterData * chr;
	NNSG2dImageProxy * ipc;
	void * buf;
	u32	cgx;
	u32	i;
	MAIL_ICON	mi;

	md = syswk->app->mail[ syswk->lst_pos ];

	for( i=0; i<MAILDAT_ICONMAX; i++ ){
		mi.dat = MailData_GetIconParamByIndex( md, i, MAIL_ICONPRM_ALL, MailData_GetFormBit(md) );
		if( mi.dat == MAIL_ICON_NULL || mi.cgxID == NARC_poke_icon_poke_icon_000_NCGR ){
			MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_POKEICON1+i, FALSE );
			continue;
		}

		ipc = CLACT_ImageProxyGet( syswk->app->cap[MBMAIN_OBJ_POKEICON1+i]->act );
		cgx = NNS_G2dGetImageLocation( ipc, NNS_G2D_VRAM_TYPE_2DSUB );
		buf = ArcUtil_HDL_CharDataGet( syswk->app->pokeicon_ah, mi.cgxID, 0, &chr, HEAPID_MAILBOX_APP );
		DC_FlushRange( chr->pRawData, POKEICON_CGX_SIZE );
		GXS_LoadOBJ( chr->pRawData, cgx, POKEICON_CGX_SIZE );
		sys_FreeMemoryEz( buf );

		CATS_ObjectPaletteSetCap( syswk->app->cap[MBMAIN_OBJ_POKEICON1+i], mi.palID );
		MBOBJ_Vanish( syswk->app, MBMAIN_OBJ_POKEICON1+i, TRUE );
	}
}


//============================================================================================
//	その他のＯＢＪ
//============================================================================================

//--------------------------------------------------------------------------------------------
/**
 * メールボックス画面のリソース読み込み
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void MailBoxObjResLoad( MAILBOX_APP_WORK * appwk )
{
	ARCHANDLE * ah = ArchiveDataHandleOpen( ARC_MAILBOX_GRA, HEAPID_MAILBOX_APP );

	CATS_LoadResourceCharArcH(
		appwk->csp, appwk->crp, ah,
		NARC_mailbox_gra_mailbox_obj_lz_NCGR,
		1, NNS_G2D_VRAM_TYPE_2DMAIN, CHR_ID_MBOBJ );

	CATS_LoadResourcePlttArcH(
		appwk->csp, appwk->crp, ah,
		NARC_mailbox_gra_mailbox_obj_NCLR, 0,
		PALSIZ_MBOBJ, NNS_G2D_VRAM_TYPE_2DMAIN, PAL_ID_MBOBJ );

	CATS_LoadResourceCellArcH(
		appwk->csp, appwk->crp, ah,
		NARC_mailbox_gra_mailbox_obj_lz_NCER, 1, CEL_ID_MBOBJ );

	CATS_LoadResourceCellAnmArcH(
		appwk->csp, appwk->crp, ah,
		NARC_mailbox_gra_mailbox_obj_lz_NANR, 1, ANM_ID_MBOBJ );

	ArchiveDataHandleClose( ah );
}

//--------------------------------------------------------------------------------------------
/**
 * 「メールをよむ」カーソルセット
 *
 * @param	appwk		アプリワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void MBOBJ_MailReadCurMove( MAILBOX_APP_WORK * appwk )
{
	MBOBJ_PosSet( appwk, MBMAIN_OBJ_CURSOR, MAILREAD_CUR_PX, MAILREAD_CUR_PY );
	MBOBJ_AnmSet( appwk, MBMAIN_OBJ_CURSOR, 4 );
}